<template>
  <div>
    <el-row :gutter="40" type="flex">
      <el-col :xs="10" :sm="12" :md="14" :lg="20">
        <el-col :xs="10" :sm="20" :md="18" :lg="10">
          <el-form :model="passwordForm" :rules="rules" ref="passwordFormRef" label-width="110px"
            label-position="right">
            <el-form-item label="旧密码" prop="oldPassword">
              <el-input type="password" v-model="passwordForm.oldPassword" autocomplete="off" clearable></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="password">
              <el-input type="password" v-model="passwordForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="rePassword">
              <el-input type="password" v-model="passwordForm.rePassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size="small" type="primary" @click="savePassword" :loading="btnLoading">保存</el-button>
              <el-button size="small" @click="reset">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import { updatePassword } from "@/api/member/index";
import Cookies from "js-cookie";
export default {
  props: {

  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请设置新密码'));
      } else {
        if (this.passwordForm.rePassword !== '') {
          this.$refs.passwordFormRef.validateField('rePassword');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.passwordForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      btnLoading: false,
      passwordForm: {
        oldPassword: '',
        password: '',
        rePassword: ''
      },
      rules: {
        oldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' },
          { min: 6, max: 18, message: '请输入6-18个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur' },
          { min: 6, max: 18, message: '请输入6-18个字符', trigger: 'blur' }
        ],
        rePassword: [
          { required: true, validator: validatePass2, trigger: 'blur' },
          { min: 6, max: 18, message: '请输入6-18个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 保存密码
    savePassword() {
      this.$refs.passwordFormRef.validate(valid => {
        if (valid) {
          this.btnLoading = true;
          const data = {
            oldPassword: this.passwordForm.oldPassword,
            newPassword: this.passwordForm.password,
            id: JSON.parse(Cookies.get("userInfo")).userId,
          };

          updatePassword(data).then((res) => {
            this.$message({
              message: "密码修改成功",
              type: "success",
            });
            this.btnLoading = false;
          }).catch((err) => {
            this.btnLoading = false;
          });

        }
      })

    },
    // 重置表单
    reset() {
      this.$refs.passwordFormRef.resetFields()
    }

  },
  components: {
  }
};
</script>
